﻿using ML.Entity;
using ML.IBusiness;
using ML.Plug;
using System.Web.Mvc;

namespace ML.Web.Controllers
{
    /// <summary>
    /// 主页面控制器
    /// </summary>
    [Login]
    public class HomeController : BaseController
    {
        private readonly ICapitalAccountBusiness capitalAccountBusiness;
        public HomeController(ICapitalAccountBusiness _capitalAccountBusiness)
        {
            capitalAccountBusiness = _capitalAccountBusiness;
        }

        /// <summary>
        /// 后台管理登录页面
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Index()
        {
            //查询商户下和管理员下的汇总信息返回
            var merchantId = "-1";
            var userInfo = UserHelper.GetCurrentUser();
            if (userInfo.UserType == (int)UserTypeStatus.admin)
            {
                merchantId = "-1";
            }
            if (userInfo.UserType == (int)UserTypeStatus.merchant)
            {
                merchantId = userInfo.MerchantId;
            }

            //查询数据库
            var data = capitalAccountBusiness.GetCollectInfo(merchantId);
            ViewBag.Balance = data.Balance;
            ViewBag.Withdrawed = data.Withdrawed;
            ViewBag.ResidueMoney = data.ResidueMoney;
            ViewBag.OrderCount = data.OrderCount;
            return View();
        }

        /// <summary>
        /// 模板表头页面
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Head()
        {
            //获取当前请求的路径,传递页面信息查询权限
            UserInfoSession userInfoSession = UserHelper.GetCurrentUser();
            ViewBag.NickName = userInfoSession == null ? "" : userInfoSession.NickName;
            ViewBag.UserImage = userInfoSession == null || string.IsNullOrEmpty(userInfoSession.PhotoGraph) ? "/Content/images/default.jpg" : userInfoSession.PhotoGraph;
            return PartialView("_Head");
        }

        /// <summary>
        /// 左侧菜单页面
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult MenuInfo()
        {
            UserInfoSession userInfoSession = UserHelper.GetCurrentUser();
            ViewBag.UserType = userInfoSession.UserType;
            return PartialView("_MenuInfo");
        }

        /// <summary>
        /// 主题页面
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Theme()
        {
            return PartialView("_Theme");
        }

        /// <summary>
        /// 退出用户(删除Session)
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Layout()
        {
            UserHelper.RemoveCurrentUser();
            return Redirect("/Login/Index");
        }
    }
}